package de.hauck.rtmidlet.views;

import java.util.Vector;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.model.StockBean;
import de.hauck.rtmidlet.utils.MyLogger;

public class FormLinksMaintain extends Form implements CommandListener{
	private Displayable _dble;
	private Command cSave = new Command("Save", Command.OK, 1);
	private Command cDeleteYes = new Command("Yes", Command.OK, 1);
	private Command cDeleteNo = new Command("No", Command.BACK, 1);
	private Command cCancel = new Command("Cancel", Command.BACK, 1);
	
	private TextField newChartLink, newChartType;
	private ChoiceGroup defaultSelected;
	private ChoiceGroup urlGroup;
	private final int ADD=0;
	//private final int DEL=1;
	private int _type;
	
	public FormLinksMaintain(Displayable dble, int type) {
		super("Maintain Chart Links");
		_dble=dble;
		_type=type;
		if (type==ADD) {
			newChartType=new TextField("Description", "", 255, TextField.ANY);
			newChartLink=new TextField("URL", "", 255, TextField.ANY);
			append(newChartType);
			append(newChartLink);
			defaultSelected = new ChoiceGroup("Default select", ChoiceGroup.MULTIPLE);
			defaultSelected.append("yes", null);
			append(defaultSelected);
		} else {
			urlGroup = new ChoiceGroup("Delete", ChoiceGroup.MULTIPLE);
			for (int i=0; i<Configuration.CHART_URLS.length; i++) {
				urlGroup.append("Charts Link "+Configuration.getAvailableChartTypes()[i], null);	
			}
			append(urlGroup);
		}
		addCommand(cSave);
		addCommand(cCancel);
		setCommandListener(this);
	}

	public void commandAction(Command command, Displayable dble) {
		if (command==cSave) {
			if (_type==ADD) {
				MyLogger.debug("Setting new chart url settings (ADD)");
				Configuration.dirtyFlagConfig=true;
				Configuration.addNewChartLink(newChartType.getString(),newChartLink.getString());
				Vector stocks = RTMidlet.getInstance().getStockStore().getStocksVector();
				for (int j=0; j<stocks.size(); j++) {
					StockBean bean=(StockBean)stocks.elementAt(j);
					if (defaultSelected.isSelected(0)) bean.addEntry(true); 
					else bean.addEntry(false);
				}
				RTMidlet.getInstance().show(_dble);
			}	
			else {
				showDeleteImportConfirmation();
			}
		} else if (command==cDeleteYes) {
			MyLogger.debug("Setting new chart url settings (DEL)");
			Configuration.dirtyFlagConfig=true;
			
			Vector newUrls = new Vector();
			Vector newTypes = new Vector();
			
			for (int i = 0; i < urlGroup.size(); i++) {
				if (!urlGroup.isSelected(i)) {
					newTypes.addElement(Configuration.getAvailableChartTypes()[i]);
					newUrls.addElement(Configuration.CHART_URLS[i]);
				} else {
					Vector stocks = RTMidlet.getInstance().getStockStore().getStocksVector();
					for (int j=0; j<stocks.size(); j++) {
						StockBean bean=(StockBean)stocks.elementAt(j);
						bean.deleteEntriesAt(i);
					}
				}
			}
			Configuration.CHART_URLS=new String[newUrls.size()];
			newUrls.copyInto(Configuration.CHART_URLS);
			String[] newTypesArray=new String[newTypes.size()];
			newTypes.copyInto(newTypesArray);
			Configuration.setAvailableChartTypes(newTypesArray);
			RTMidlet.getInstance().show(_dble);	
		} else if (command==cDeleteNo) { 
			RTMidlet.getInstance().show(_dble);
		} else if (command == cCancel) {
			RTMidlet.getInstance().show(_dble);
		} 		
	}
	
	public void showDeleteImportConfirmation() { 
		Alert import_confirm = new Alert(
				"Import",
				"Do you really want to delete the selected links?",
				null, AlertType.CONFIRMATION);
		import_confirm.addCommand(cDeleteYes);
		import_confirm.addCommand(cDeleteNo);
		import_confirm.setCommandListener(this);	
		RTMidlet.getInstance().show(import_confirm);
	}

}
